package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.Video;
import com.yantong.mapper.VideoMapper;
import com.yantong.service.VideoService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenhu
 * @Date: 2020/08/03 15:34
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 列表
     */
    @Override
    public PageUtils<Video> videoTypeList(String queryItem, Long centreId, String video_name, int offset, int limit){
        Map<String,Object> params = new HashMap<String,Object>();
        if (StringUtils.isNotEmpty(video_name)) {
            params.put("video_name", video_name);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(centreId)) {
            params.put("centreId", centreId);
        }
        PageUtils<Video> pageUtils = new PageUtils<>();
        int total = videoMapper.countByParams(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(videoMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    /**
     * 修改
     * @param video
     * @return
     */
    @Override
    public int updateVideoType(Video video) {
        if (video == null || video.getVideo_id() == null) {
            return 0;
        }
        return videoMapper.updateVideoType(video);
    }

    /**
     * 新增
     * @param video
     * @return
     */
    @Override
    public int addVideoType(Video video) {
        if (video == null) {
            return 0;
        }
        return videoMapper.addVideoType(video);
    }

    /**
     * 根据主键获取视频类型
     * @param video_id
     * @return
     */
    @Override
    public Video getVideoTypePrimaryKey(Long video_id) {
        if (video_id == null) {
            return null;
        }
        return videoMapper.getVideoTypePrimaryKey(video_id);
    }

    /**
     * 视频类型转为map
     */
    @Override
    public Map<String, Object> getVideoTypeListToMap(Long centreId) {
        Map<String,Object> map = new HashMap<String, Object>();
        Map<String,Object> params = new HashMap<String,Object>();
        if (ValidateUtil.isNotEmptyObjectOrString(centreId)){
            params.put("centreId",centreId);
        }
        List<Video>  list = videoMapper.listByParams(params,new RowBounds());
        map.put("videoType",list);
        return map;
    }

    /**
     * 获取视频列表
     */
    @Override
    public PageUtils<Video> videoList(String title,Long centre_id,String video_id, int offset, int limit) {
        Map<String,Object> params = new HashMap<String,Object>();
        if (StringUtils.isNotEmpty(title)) {
            params.put("title", title);
        }
        if (StringUtils.isNotEmpty(video_id)) {
            params.put("video_id", video_id);
        }
        if (ValidateUtil.isNotEmptyObjectOrString(centre_id)){
            params.put("centreId",centre_id);
        }
        PageUtils<Video> pageUtils = new PageUtils<>();
        int total = videoMapper.countByVideoList(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(videoMapper.videoList(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    /**
     * 新增
     */
    @Override
    public int addVideo(Video video) {
        if (video == null) {
            return 0;
        }
        return videoMapper.addVideo(video);
    }

    /**
     * 修改
     */
    @Override
    public Video getVideoPrimaryKey(Long id) {
        if (id == null) {
            return null;
        }
        return videoMapper.getVideoPrimaryKey(id);
    }

    /**
     * 修改
     * @param video
     * @return
     */
    @Override
    public int updateVideo(Video video) {
        if (video == null || video.getId()== null) {
            return 0;
        }
        return videoMapper.updateVideo(video);
    }

    /**
     * 修改
     * @param video
     * @return
     */
    @Override
    public int updateVideoAddOne(Video video) {
        if (video == null || video.getId()== null) {
            return 0;
        }
        return videoMapper.updateVideoAddOne(video);
    }

}
